package com.coin900.coin.server.fu.ctrl;

import com.coin900.coin.persist.fu.FuUserVO;
import com.coin900.coin.server.BaseController;
import com.coin900.coin.service.fu.IFuUserService;
import com.value.exception.BusinessException;
import com.value.utils.Md5Encoder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * @author shui
 * @create 2017-11-21
 **/
@Controller
public class FuCtrl extends BaseController {

    private static final Log logger = LogFactory.getLog(BaseController.class);

    /**
     * 用户注册
     *
     * @param fuUserVO
     * @param request
     * @return
     */
    @RequestMapping(value = "/fu/users", method = RequestMethod.POST)
    public ModelAndView createFuUser(@RequestBody FuUserVO fuUserVO,
                                     HttpServletRequest request) {

        Map<String, Object> params = new HashMap<>();
        String mobile = fuUserVO.getMobile();
        params.put("mobile", mobile);
        List<FuUserVO> fuUserVOS = this.userService.loadFuUser(params);
        if (!fuUserVOS.isEmpty()) {
            throw new BusinessException("手机号已注册");
        }

        String username = fuUserVO.getUsername();
        params.put("username", username);
        List<FuUserVO> fuUserVOS1 = this.userService.loadFuUser(params);
        if (!fuUserVOS1.isEmpty()) {
            throw new BusinessException("用户名已注册");
        }


        // 用户名是否存在


        // 验证码校验


        // 短信验证码校验


        // 创建用户
        FuUserVO userVO = new FuUserVO();
        userVO.setUuid(UUID.randomUUID().toString());
        userVO.setUsername(username);
        userVO.setPassword(Md5Encoder.generateCode(fuUserVO.getPassword()));
        userVO.setMobile(mobile);
        userVO.setName(mobile);
        userVO.setCreatedBy(0L);
        userVO.setModifiedBy(0L);
        userVO.setCreatedDate(new Date());
        userVO.setModifiedDate(new Date());
        userVO.setUserStatus(1);
        userVO.setDataStatus(1);


        return new ModelAndView("index");
    }

    @Autowired
    private IFuUserService userService;
}
